package com.gms.service.equipmanager.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gms.entity.equipment.Equipmentrent;
import com.gms.mapper.EquipmentrentMapper;
import com.gms.service.equipmanager.EquipmentrentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jianghai
 * @since 2021-06-18
 */
@Service
public class EquipmentrentServiceImpl extends ServiceImpl<EquipmentrentMapper, Equipmentrent> implements EquipmentrentService {

    @Autowired
    private EquipmentrentMapper mapper;

    //private User user;

    @Override
    public void addEquipmentOrder(Equipmentrent equipmentrent) {
        equipmentrent.setStatus("0");   //表示租借中
        //equipmentrent.setUserName(user.getUserName());
        mapper.insert(equipmentrent);
    }

    @Override
    public void deleteEquipmentOrder(Integer eRentId) {
        mapper.deleteById(eRentId);
    }

    @Override
    public void alterEquipmentOrder(Integer eRentId) {
        Equipmentrent equipmentrent = mapper.selectById(eRentId);
        //器材操作
        equipmentrent.setReceiveTime(new Date());
        equipmentrent.setStatus("已回收"); //已回收
        mapper.updateById(equipmentrent);
    }

    @Override
    public Equipmentrent selectOneEquipmentOrder(Integer erentid) {
        QueryWrapper<Equipmentrent> wrapper = new QueryWrapper<>();
        wrapper.eq("erentid",erentid);
        Equipmentrent equipmentrent = mapper.selectOne(wrapper);
        return equipmentrent;
    }

    @Override
    public Page<Equipmentrent> pageRent(Integer pageNum, String userName, Integer uId) {
        Page<Equipmentrent> page = new Page<>(pageNum, 5);
        QueryWrapper<Equipmentrent> wrapper = new QueryWrapper<>();
        if (uId != null) {
            wrapper.eq("uId",uId);
        }
        if (!StringUtils.isEmpty(userName)) {
            wrapper.like("userName",userName);
        }
        wrapper.eq("status","0");   //表示租借中
        return mapper.selectPage(page,wrapper);
    }

    @Override
    public Page<Equipmentrent> pageAllOrder(Integer pageNum, String userName, Integer uId) {
        Page<Equipmentrent> page = new Page<>(pageNum,5);
        QueryWrapper<Equipmentrent> wrapper = new QueryWrapper<>();
        if (uId != null) {
            wrapper.eq("uId",uId);
        }
        if (!StringUtils.isEmpty(userName)) {
            wrapper.like("userName",userName);
        }
        return mapper.selectPage(page, wrapper);
    }

    @Override
    public void delete_orders(String etypename) {
        UpdateWrapper<Equipmentrent> wrapper = new UpdateWrapper<>();
        wrapper.eq("etypename",etypename);
        mapper.delete(wrapper);
    }

}